package dlg.code.composite.service.impl;

import dlg.code.composite.model.TreeNode;
import dlg.code.composite.model.aggregates.DecisionTree;

import java.util.List;
import java.util.Map;

public class DecisionEngine {
    private final DecisionTree decisionTree;

    public DecisionEngine(DecisionTree decisionTree) {
        this.decisionTree = decisionTree;
    }

    public TreeNode decision(Map<String, String> matterMap) {
        TreeNode root = decisionTree.getRoot();
        while (!root.isLeaf()) {
            String decisionType = root.getDecisionType();
            String v = matterMap.get(decisionType);

            List<TreeNode> childList = root.getChildList();
            for (int i = 0; i < childList.size(); i++) {
                if (childList.get(i).getCondition().predict(childList.get(i).getV(), v)) {
                    root = childList.get(i);
                    break;
                }
            }
        }
        return root;
    }
}
